#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

#define SIZE 1024
#define MAX_ARGV 64
#define SEP " "


char* argv[MAX_ARGV]; 
char pwd[SIZE];
char env[SIZE];
int lastcode = 0;

const char* HostName()
{
    char* hostname = getenv("HOSTNAME");
    if(hostname)
        return hostname;
    else
        return "None";
}

const char* UserName()
{

    char* username = getenv("USER");
    if(username)
        return username;
    else
        return "None";
}

const char* CurrentWorkDir()
{

    char* pwd = getenv("PWD");
    if(pwd)
        return pwd;
    else
        return "None";
}

char* Home()
{
    return getenv("HOME");
}
int Interactive(char out[], int size)
{
    
    // 输出提示符并获取用户输入的命令字符串"ls -a -l"
    printf("[%s@%s %s]$ ",UserName(), HostName(), CurrentWorkDir());
    fgets(out, size, stdin);
    out[strlen(out) - 1] = 0;
    return strlen(out);
}

void Split(char in[])
{
    int i = 0;
    argv[i++] = strtok(in, SEP);
    while(argv[i++] = strtok(NULL, SEP));// == 故意写成 =
    if(strcmp(argv[0], "ls") == 0)
    {
        argv[i-1] = (char*)"--color";
        argv[i] = NULL;
    }
}

void Execute()
{

     pid_t id = fork();
     if(id == 0)
     {
        //让子进程执行命名
         execvp(argv[0],argv);
         exit(1);        
     }
    int status = 0;
    pid_t rid =  waitpid(id, NULL, 0);
    if(rid == id) lastcode = WEXITSTATUS(status);
    // printf("run done, rid: %d\n", rid);
}

int BuildinCmd()
{
    int ret = 0;
    //1.检测是否是内建命令，是1，否0
    if(strcmp("cd", argv[0]) == 0)
    {
        //2.执行
        ret = 1;
        char* target = argv[1]; // cd xxx or cd
        if(!target)
        {
            target = Home();
        }
        chdir(target);
        char temp[1024];
        getcwd(temp,1024);

        snprintf(pwd, SIZE, "PWD=%s",temp);
        putenv(pwd);
    }
    else if(strcmp("export", argv[0]) == 0)
    {
        ret = 1;
        if(argv[1]) 
        {
            strcpy(env, argv[1]);
            putenv(argv[1]);
        }
    }
    else if(strcmp("echo", argv[0]) == 0)
    {
        ret = 1;
        if(argv[1] == NULL) 
        {
            printf("\n");
        }
        else
        {
            if(argv[1][0] == '$' )
            {
                if(argv[1][1] == '?')
                {
                    printf("%d\n",lastcode);
                    lastcode = 0;
                }
                else
                {
                    char* e = getenv(argv[1] + 1);
                    if(e)  printf("%s\n",e);
                }
            }
            else
            {
                printf("%s\n",argv[1]);
            }
        }
    }
    return ret;
}

int main()
{
    while(1)
    {

        //1.打印命令行提示符，获取用户输入命令的字符串
        char commandline[SIZE];
        int n = Interactive(commandline, SIZE);
        if(n == 0) continue;    
        //2.对命令行字符串进行切割
        Split(commandline);
        //3.处理内建命令
        n = BuildinCmd();
        if(n) continue;
        // 4.执行这个命令
        Execute();

        //for(int i=0; argv[i]; i++)
        //{
        //    printf("argv[%d]: %s\n",i, argv[i]);
        //}
    }
    
    return 0;
}
